local json = require "cjson"

--返回数据方法
local function responseMsg(code, msg, data)
    local rtn = {}
    rtn['code'] = code
    rtn['msg'] = msg
    rtn['data'] = data
    return json.encode(rtn)
end

local function getRoundStatus(startTimestamp, endTimestamp)
    local currentTimestamp = os.time()
    local dataStatus = 0
    if currentTimestamp < startTimestamp then
        dataStatus = 1
    elseif currentTimestamp >= startTimestamp and currentTimestamp <= endTimestamp then
        dataStatus = 2
    else
        dataStatus = 3
    end
    return dataStatus
end

ngx.header["Access-Control-Allow-Origin"] = "*"
ngx.header["Access-Control-Allow-Headers"] = "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range, userid, agent, brandid, language, token"

--验证参数
local arg = ngx.req.get_uri_args()
local seckillId = arg.seckill_id
if not seckillId or #seckillId <= 0 then
    ngx.say(responseMsg('-1', 'seckill_id参数有误', nil))
    return
end

-- 数据库、redis 等基础配置项
local env = ''
local mysql_prop = {}
local redis_prop = {}
local redis_prop_home = {}
if env == 'local' then
    mysql_prop = {
        host = "zljsz-testenvironmentdb.mysql.rds.aliyuncs.com",
        port = 3306,
        database = "panda_recovery",
        user = "code_test",
        password = "reVvtDiN2g3JaCxc"
    }

    redis_prop = {
        host = "zljsz-testenvironmentdb.redis.rds.aliyuncs.com",
        port = 6379,
        auth = 'lzWu9gskZ9OQrnOP'
    }
    redis_prop_home = {
        host = "zljsz-testenvironmentdb.redis.rds.aliyuncs.com",
        port = 6379,
        auth = 'lzWu9gskZ9OQrnOP'
    }
elseif env == 'test' then
    mysql_prop = {
        host = "zljsz-testenvironmentdb.mysql.rds.aliyuncs.com",
        port = 3306,
        database = "panda_recovery",
        user = "code_test",
        password = "reVvtDiN2g3JaCxc"
    }

    redis_prop = {
        host = "zljsz-testenvironmentdbvpc.redis.rds.aliyuncs.com",
        port = 6379,
        auth = 'lzWu9gskZ9OQrnOP'
    }
    redis_prop_home = {
        host = "zljsz-testenvironmentdbvpc.redis.rds.aliyuncs.com",
        port = 6379,
        auth = 'lzWu9gskZ9OQrnOP'
    }
else
    mysql_prop = {
        host = "rm-wz9s54k9r047b5012rw.mysql.rds.aliyuncs.com",
        port = 3306,
        database = "panda_recovery",
        user = "code_recovery",
        password = "xGvyhcnXL49RWz1N"
    }

    redis_prop = {
        host = "zljsz-recoverydbvpc.redis.rds.aliyuncs.com",
        port = 6379,
        auth = 'NeSt6Wauy3ZKqEMR'
    }
    redis_prop_home = {
        host = "zljsz-redis1dbvpc.redis.rds.aliyuncs.com",
        port = 6379,
        auth = 'JtS42CKZsepk88EX'
    }
end

--连接redis
local redis = require "resty.redis"
zljRed = redis:new()
local ok, err = zljRed:connect(zljRedisConfig['host'], zljRedisConfig['port'])   --连接redis
if not ok then
    ngx.say(responseMsg('-1', json.encode(err), nil))
    return
end
--连接redis密码
ok, err = zljRed:auth(zljRedisConfig['auth'])   --连接redis
if not ok then    -- 链接失败的时候
    ngx.say(responseMsg('-1', json.encode(err), nil));
    return
end

local token = arg.token
local userId = 0;
if token and #token > 0 then
    userId = zljRed:get(token);
end
if userId == ngx.null then
    ngx.say(responseMsg('-1', "用户token已过期", nil));
    return
end

red = redis:new()
local ok, err = red:connect(recoveryRedisConfig['host'], recoveryRedisConfig['port'])   --连接redis
if not ok then
    ngx.say(responseMsg('-1', json.encode(err), nil))
    return
end
--连接redis密码
ok, err = red:auth(recoveryRedisConfig['auth'])   --连接redis
if not ok then    -- 链接失败的时候
    ngx.say(responseMsg('-1', json.encode(err), nil));
    return
end

red:select(1)

local cacheKey = "zlj:jqq:activity:seckill:entity:"..seckillId
local data = red:get(cacheKey)
if data == ngx.null then
    ngx.say(responseMsg("-1", "seckill不存在", nil))
    return
end

seckillData = json.decode(data)
if seckillData["rounds"] then
    for k, v in pairs(seckillData["rounds"]) do
        seckillData["rounds"][k]["status"] = getRoundStatus(v['start_timestamp'], v['end_timestamp'])
    end
end

ngx.header.content_type = "application/json"
ngx.say(responseMsg("1", "", seckillData))
return
